import {IExecute} from '../methodExceuteProvider';
import {CONSTKEY, getTemplate, COMPTYPE, convertArgToArray} from '../utils';
import {BaseEasyuiComp} from './baseEasyuiComp';
import { ExtendBind } from './extendbind';
 
export  class EasyuiHelper{
    static createEasyui(compName:string, props:Array<string>, methods, execute:IExecute, extendBind:ExtendBind):any{
        let tmpClass = class extends BaseEasyuiComp { 
            public $dom:JQuery;
            public params: any;
            constructor(params, componentConfig){ 
                super(compName.toLowerCase(), props, componentConfig);
                this.params = params;
                this.$dom = $($(componentConfig.element)[0].firstChild);
                let that = this;
                //绑定方法，方法还需要继承组件支持的方法的绑定
                Object.getOwnPropertyNames(methods).forEach(index=>{
                    if(!$.isNumeric(index)) return true;
                    let methodName = methods[index]; 
                    this[methodName] = function(){
                        //需要加一个拦截机制，用于处理依赖组件的方法（如：combogrid，依赖grid，那么grid的方法怎么执行）
                        let args = convertArgToArray.call(arguments);
                        //传入的参数进行扩展
                        extendBind.extend(this.name, args[0], this.koContext); 
                        args.unshift(methodName);
                        return execute.execute(that.name, methodName, that.$dom, args); 
                    }; 
                }); 
            }   
            
            /**
             * 绑定change事件
             */
            private bindChangeEvent(sourceopts, opt:any){ 
                let changeEvent = props.find((item)=>{
                    return item == 'onChange' ? true : false;
                });
                if(sourceopts.value && ko.isObservable(sourceopts.value) && changeEvent){
                    let that = this;
                    opt['onChange'] = function(newValue, oldValue){
                        sourceopts.value(newValue); //这里会影起reflow。
                    }
                }
            }
            
            protected createOptions(options){
                let opt = this.createOptionsCore(options);
                if(options){
                    this.bindChangeEvent(options, opt);
                }    
                debugger
                extendBind.extend(this.name, opt, this.koContext); 
                return opt;
            } 
            public repaint(options:any){
                this.resetComp(options);
                this.paint(options);
            }
            public dispose(){
                console.log('dispose ' + this.name);
                if(this.params.options.dispose){
                    this.params.options.dispose.apply(this);
                }else{
                    //@ts-ignore
                    this.destroy && this.destroy();
                }
            }
        };

        return tmpClass;
    }
}
/**
 * 根据dom获取上下文
 * @param dom dom节点 
 */
export function getContextFor(dom:HTMLElement){
    let comp = $.data(dom);
    return comp ? comp.$data : null;
}